package org.peterf.foxish.component.updater;

import org.peterf.foxish.component.property.Updatable;
import org.peterf.foxish.component.updater.interfaces.Updater;
import org.peterf.foxish.engine.input.FingerEvent;
import org.peterf.foxish.engine.input.OnTouchListener;

import android.view.MotionEvent;

public class OnTouchUpdater implements Updater, OnTouchListener {

    private Updater updater;

    private boolean working = false;

    public OnTouchUpdater(Updater updater) {
        this.updater = updater;
    }

    public boolean isFinished() {
        return false;
    }

    public boolean onTouch(FingerEvent event) {
        // TODO Should return false in some situations.
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            updater.reset();
            working = true;
            return true;
        }
        return true;
    }

    public void reset() {
        updater.reset();
    }

    public void update(Updatable updatable, float elapsed) {
        if (working) {
            if (updater.isFinished()) {
                working = false;
                return;
            }
            updater.update(updatable, elapsed);
        }
    }

}
